package 第二十章项目.clothesSystem.service.impl;

import 第二十章项目.clothesSystem.bean.User;
import 第二十章项目.clothesSystem.service.UserService;
import 第二十章项目.clothesSystem.ui.UserIO;
import 第二十章项目.clothesSystem.ui.Welcome;
import 第二十章项目.clothesSystem.utils.BusinessException;
import 第二十章项目.clothesSystem.utils.EmptyUtils;


public class UserServiceImpl implements UserService {
    @Override
    public User register(User user) throws BusinessException {
        UserIO userIO=new UserIO();
        if(EmptyUtils.isEmpty(user.getUsername())){
            throw new BusinessException("username.notnull");
        } else if (EmptyUtils.isEmpty(user.getPassword())) {
            throw new BusinessException("password.notnull");
        }
        if(!EmptyUtils.isRepeat(user.getUsername())) {

            userIO.addUsers(user);
            userIO.writeUsers();
        }else{
            Welcome.println(Welcome.getString("userData.repeat.error"));
            try {
                Welcome.class.newInstance().start();
            } catch (InstantiationException | IllegalAccessException e) {
                throw new RuntimeException(e);
            }
        }
        return user;
    }

    @Override
    public User login(String username, String password) throws BusinessException {

        if(EmptyUtils.isEmpty(username)){
            throw new BusinessException("username.notnull");
        } else if (EmptyUtils.isEmpty(password)) {
            throw new BusinessException("password.notnull");
        }
        UserIO userio=new UserIO();
        User user = userio.findbyUsernameandPassword(username, password);
        //System.out.println("ok"+user.getUsername());
        return user;
    }
}
